package types

import (
	"encoding/xml"
	"strings"
)

var (
	StatusDisabled     = Status("disabled")
	StatusFault        = Status("fault")
	StatusDown         = Status("down")
	StatusUnackedAlerm = Status("unackedAlerm")
	StatusAlarm        = Status("alarm")
	StatusUnacked      = Status("unacked")
	StatusOverridden   = Status("overridden")
	StatusOk           = Status("ok")
	StatusUnknown      = Status("unknown")
)

type Status string

func (a Status) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	var s string
	switch a {
	default:
		s = string(StatusUnknown)
	case StatusDisabled,
		StatusFault,
		StatusDown,
		StatusUnackedAlerm,
		StatusAlarm,
		StatusUnacked,
		StatusOverridden,
		StatusOk:
		s = string(a)
	}
	return e.EncodeElement(s, start)
}

func (a *Status) UnmarhshalXML(d *xml.Decoder, start xml.StartElement) error {
	var s string
	if err := d.DecodeElement(&s, &start); err != nil {
		return err
	}
	switch s := strings.ToLower(s); s {
	default:
		*a = StatusUnknown
	case "disabled",
		"fault",
		"down",
		"unackedAlerm",
		"alarm",
		"unacked",
		"overridden",
		"ok":
		*a = Status(s)
	}
	return nil
}
